package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.exception.CustomException;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Tang
 * @date 2023/4/17
 */
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //1.调用service的新增方法
        shoppingCartService.add(shoppingCart);

        //2.返回结果
        return R.success("添加购物车成功！");
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //1.获取当前登录的用户id
        Long userId = BaseContext.getValue();
        if (userId == null) {
            throw new CustomException("请先登录！");
        }

        //2.根据用户id查询购物车列表数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        //3.调用service查询方法
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        //4.返回结果
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //1.获取当前登录的用户id
        Long userId = BaseContext.getValue();
        if (userId == null) {
            throw new CustomException("请先登录！");
        }

        //2.创建查询对象，设置查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        //3.调用service的删除方法
        boolean flag = shoppingCartService.remove(wrapper);

        //4.返回结果
        return flag ? R.success("清空成功！") : R.error("清空失败！");
    }
    @PostMapping("/sub")//减少购物车或者菜单栏中菜品或者套餐的数量
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            //老方法注释这一行

            lqw.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one==null){
            return R.error("已经是零份了请刷新页面");
        }
        Integer number = one.getNumber();
        if (number!=null){
            if(number-1==0){
                shoppingCartService.removeById(one);
                one.setNumber(0);
                return R.success(one);
            }
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        return R.success(one);
    }
}
